<?php
namespace app\common\service;

use addons\epay\library\Service;
use app\common\model\User;
use addons\epay\library\Yansongda\Pay\Pay as Yansongda;

class Pay
{
    public static function refund($order,$desc,$money=0){
        //$order=Order::get(['order_id'=>$order_id]);
        if($order['refund_status']==1){
            return false;
        }

        //if($order['payment_type']=='wechat'){
            $config=Service::getConfig('wechat');
            $pay=Yansongda::wechat($config);
            $refund_order = [
                'out_trade_no'  => $order['order_sn'],
                'out_refund_no' => 'R'.$order['order_sn'].'-'.time(),
                'total_fee'     => $order['pay_price'] * 100,
                'refund_fee'    => $money?$money*100:$order['pay_price'] * 100,
                'refund_desc'   => $desc,
                'type'          => 'miniapp'
            ];
        /*}
        else if($order['payment_type']=='alipay'){
            $refund_order = [
                'out_trade_no' => $order->order_sn,
                'refund_amount' => $money?:$order['order_amounts'],
                'out_request_no'=>$order->order_sn
            ];
            $config=Service::getConfig('alipay');
            $pay=Yansongda::alipay($config);
        }*/

        $refund = $pay->refund($refund_order);
        $refund = json_decode($refund, true);
        adlog('退款', $refund);
        if ($refund && ((isset($refund['return_code']) && $refund['return_code'] == 'SUCCESS')||(isset($refund['msg']) && $refund['msg'] == 'Success'))) {
            return true;
        }
        return false;
    }

    /**
     * 根据退款金额收回各级提成
     * @param object $order 订单id
     * @param float $money 退款金额
     * @return bool
     */
    public static function revokeMoney($order, float $money): bool
    {
        $last_money=$money;

        //扣除推荐人分成
        if($order['inviter_money']){
            $parent=User::get(['id'=>$order['inviter_id']])?:[];
            if($last_money>$order['inviter_money']){
                if($parent&&$order->complete_time)
                    User::money($order['inviter_money'],$order['inviter_id'],'订单退款，扣除推荐人分成',$order['user_id']);
                $last_money=bcsub($last_money,$order['inviter_money'],2);
                $order['refund_parent']=$order['inviter_money'];
            }
            else{
                if($parent&&$order->complete_time)
                    User::money($last_money,$order['inviter_id'],'订单退款，扣除推荐人分成',$order['user_id']);
                $order['refund_parent']=$last_money;
                $last_money=0;
            }
        }


        //扣除平台分成
        if($order['platform_money']){
            if($last_money>$order['platform_money']){
                $last_money=bcsub($last_money,$order['platform_money'],2);
                $order['refund_platform']=$order['platform_money'];
            }
            else{
                $order['refund_platform']=$last_money;
                $last_money=0;
            }
        }

        //扣除技师收益
        if($order['worker_money']){
            $worker=User::get($order['worker_id'])?:[];

            if($last_money>$order['worker_money']){
                if($worker&&$order['finish_time'])
                    User::money($order['worker_money'],$worker['user_id'],'订单退款，扣除员工收益');
                $last_money=bcsub($last_money,$order['worker_money'],2);
                $order['refund_worker']=$order['worker_money'];
            }
            else{
                if($worker&&$order->complete_time)
                    User::money($last_money,$worker['user_id'],'订单退款，扣除员工收益');
                $order['refund_worker']=$last_money;
                $last_money=0;
            }
        }

        $order->save();
        return true;
    }
}